import 'package:core/core.dart';
import 'package:flutter/material.dart';

class TextFul extends StatelessWidget {
  final String text;
  final TextStyle? style;
  final bool required;
  final GestureTapCallback? onTap;
  final Widget? widgetStart;
  final Widget? widgetEnd;

  final double? width;
  final double? height;
  final EdgeInsets? padding;
  final EdgeInsets? margin;
  // decoration优先级大于它的子属性
  final Decoration? decoration;
  final Color? bgColor;
  // borderRadius优先级大于它的子属性
  final BorderRadiusGeometry? borderRadius;
  final double? radius;
  final Border? border;
  final AlignmentGeometry? alignment;

  const TextFul({
    super.key,
    required this.text,
    this.style,
    this.required = false,
    this.onTap,
    this.widgetStart,
    this.widgetEnd,
    this.width,
    this.height,
    this.padding,
    this.margin,
    this.decoration,
    this.bgColor,
    this.borderRadius,
    this.radius,
    this.border,
    this.alignment,
  });

  @override
  Widget build(BuildContext context) {
    Widget child = RichText(
      text: TextSpan(
        text: required ? '*' : '',
        style: Colors.red.toStyle(style?.fontSize ?? 28.px),
        children: [
          if (null != widgetStart)
            WidgetSpan(
              child: widgetStart!,
              alignment: PlaceholderAlignment.middle,
            ),
          TextSpan(text: text, style: style),
          if (null != widgetEnd)
            WidgetSpan(
              child: widgetEnd!,
              alignment: PlaceholderAlignment.middle,
            ),
        ],
      ),
    );
    if([width, height, padding, margin, decoration, bgColor, borderRadius, radius, border, alignment].elementIsNotNull){
      child = Container(
        width: width,
        height: height,
        padding: padding,
        margin: margin,
        decoration: decoration ?? BoxDecoration(
          color: bgColor,
          borderRadius: borderRadius ?? (null == radius ? null : BorderRadius.circular(radius!)),
          border: border,
        ),
        alignment: alignment,
        child: child,
      );
    }
    if(null != onTap){
      child = InkWell(
        onTap: onTap,
        child: child,
      );
    }
    return child;
  }
}
